import axios from "axios";

const axiosAPI = axios.create({
  baseURL: "/api", // it's not recommended to have this info here.
  withCredentials: true,
  timeout: 10000,
});

const apiRequest = (method, url, data) => {
  const headers = {
    authorization: "",
  };
  //using the axios instance to perform the data that received from each http method
  return axiosAPI({
    method,
    url,
    data,
    headers,
  })
    .then((res) => {
      return Promise.resolve(res.data);
    })
    .catch((err) => {
      return Promise.reject(err);
    });
};
// function to execute the http get data
const get = (url, data) => apiRequest("get", url, data);

// function to execute the http delete data
const deleteRequest = (url, data) => apiRequest("delete", url, data);

// function to execute the http post data
const post = (url, data) => apiRequest("post", url, data);

// function to execute the http put data
const put = (url, data) => apiRequest("put", url, data);

// function to execute the http path data
const patch = (url, data) => apiRequest("patch", url, data);

// expose your method to other services or actions
export { get, deleteRequest, post, put, patch };
